<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input
          v-model="queryParams.idCard"
          placeholder="请输入身份证"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:postgraduateInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:postgraduateInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:postgraduateInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:postgraduateInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postgraduateInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="研究生id" align="center" prop="postgraduateId" v-if="true"/>-->
      <el-table-column label="学号" align="center" prop="xh" />
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="照片" align="center" prop="photoPath" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.photoPathUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="研究生类型" width="100" align="center" prop="postgraduateType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_postgraduate_type" :value="scope.row.postgraduateType"/>
        </template>
      </el-table-column>
      <el-table-column label="学位类型" align="center" prop="degreeType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_degree_type" :value="scope.row.degreeType"/>
        </template>
      </el-table-column>
      <el-table-column label="身份证" align="center" prop="idCard" />
      <el-table-column label="手机号码" align="center" prop="phone" />
      <el-table-column label="邮箱" align="center" prop="email" />
      <el-table-column label="政治面貌" align="center" prop="politicalAffiliation" />
      <el-table-column label="学院名称" align="center" prop="academyName" />
      <el-table-column label="院系名称" align="center" prop="facultiesName" />
      <el-table-column label="专业名称" align="center" prop="majorName" />
      <el-table-column label="研究方向" align="center" width="180" prop="majorfieldList" >
        <template slot-scope="scope">
            <el-tag
              v-for="item in scope.row.majorfieldList"
              style="margin-top: 5px"
              :key="item"
              effect="dark">
              {{ item.fieldName }}
            </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="导师姓名" align="center" prop="expertName" />
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:postgraduateInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:postgraduateInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改研究生信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="院系" prop="facultiesId">
          <treeselect v-model="form.facultiesId" :options="facultiesOptions" :normalizer="normalizer" placeholder="请选择院系" />
        </el-form-item>
        <el-form-item label="专业" prop="majorId">
          <el-select @change="selectMajor" v-model="form.majorId" placeholder="请选择专业">
            <el-option
              v-for="item in majorList"
              :key="item.majorId"
              :label="item.majorName"
              :value="item.majorId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="研究方向" prop="majorfieldList">
          <el-select v-model="form.majorfieldList" multiple placeholder="请选择">
            <el-option
              v-for="item in majorfieldList"
              :key="item.id"
              :label="item.fieldName"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="研究生类型" prop="postgraduateType">
          <el-select v-model="form.postgraduateType" placeholder="请选择研究生类型">
            <el-option
              v-for="dict in dict.type.sys_postgraduate_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学位类型" prop="postgraduateType">
          <el-select v-model="form.degreeType" placeholder="请选择学位类型">
            <el-option
              v-for="dict in dict.type.sys_degree_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="专家导师" prop="expertId">
          <el-select v-model="form.expertId" placeholder="请选择专家导师">
            <el-option
              v-for="item in expertInfoList"
              :key="item.expertId"
              :label="item.expertName"
              :value="item.expertId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学号" prop="xh">
          <el-input v-model="form.xh" placeholder="请输入学号" />
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-input v-model="form.year" placeholder="请输入年份" />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="头像" prop="photoPath">
          <image-upload v-model="form.photoPath"/>
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请选择性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="民族" prop="nation">
          <el-input v-model="form.nation" placeholder="请输入民族" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker
            v-model="form.birth"
            type="date"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="身份证" prop="idCard">
          <el-input v-model="form.idCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="政治面貌" prop="politicalAffiliation">
          <el-input v-model="form.politicalAffiliation" placeholder="请输入政治面貌" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="homeAddress">
          <el-input v-model="form.homeAddress" placeholder="请输入家庭住址" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPostgraduateInfo, getPostgraduateInfo, delPostgraduateInfo, addPostgraduateInfo, updatePostgraduateInfo } from "@/api/system/postgraduateInfo";
import { listAcademy } from "@/api/system/academy";
import { listFaculties } from "@/api/system/faculties";
import { listMajor } from "@/api/system/major";
import { listExpertInfo } from "@/api/system/expertInfo";
import { listMajorfield } from "@/api/system/majorfield";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "PostgraduateInfo",
  components: {Treeselect},
  dicts: ['sys_user_sex','sys_postgraduate_type','sys_degree_type'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 研究生信息表格数据
      postgraduateInfoList: [],
      academyList: [],
      facultiesList: [],
      facultiesOptions: [],
      majorList: [],
      expertInfoList: [],
      majorfieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: undefined,
        academyId: undefined,
        facultiesId: undefined,
        majorId: undefined,
        expertId: undefined,
        name: undefined,
        idCard: undefined,
        phone: undefined,
      },
      // 表单参数
      form: {},
      inputVisible: false,
      inputValue: '',
      // 表单校验
      rules: {
        postgraduateId: [
          { required: true, message: "研究生id不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "blur" }
        ],
        academyId: [
          { required: true, message: "学院id不能为空", trigger: "change" }
        ],
        facultiesId: [
          { required: true, message: "院系id不能为空", trigger: "change" }
        ],
        majorId: [
          { required: true, message: "专业id不能为空", trigger: "change" }
        ],
        expertId: [
          { required: true, message: "专家导师id不能为空", trigger: "change" }
        ],
        majorfieldList: [
          { required: true, message: "研究方向不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "change" }
        ],
        idCard: [
          { pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/,
            required: true,
            message: "请输入正确的身份证号",
            trigger: "blur"
          }
        ],
        phone: [
          { pattern:/^((0\d{2,3}-\d{7,9})|(1[345789]\d{9}))$/,
            required: true,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ],
        email: [
          { pattern:/^([a-zA-Z0-9]+[-_\.]?)+@[a-zA-Z0-9]+\.[a-z]+$/,
            required: true,
            message: "请输入正确的邮箱",
            trigger: "blur"
          }
        ],
      }
    };
  },
  created() {
    this.getList();
    // this.getAcademyList();
    // this.getFacultiesList();
    this.getTreeselect();
    this.getMajorList();
    this.getExpertInfoList();
  },
  methods: {
    getAcademyList(){
      listAcademy().then(response => {
        this.academyList = response.rows;
      });
    },
    getFacultiesList(){
      listFaculties().then(response => {
        this.facultiesList = response.rows;
      });
    },
    /** 查询院系信息下拉树结构 */
    getTreeselect() {
      listFaculties().then(response => {
        this.facultiesOptions = [];
        const data = { facultiesId: 0, facultiesName: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "facultiesId", "parentId");
        this.facultiesOptions.push(data);
      });
    },
    /** 转换院系信息数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.facultiesId,
        label: node.facultiesName,
        children: node.children
      };
    },
    getMajorList(){
      listMajor().then(response => {
        this.majorList = response.rows;
      });
    },
    getExpertInfoList(){
      listExpertInfo().then(response => {
        this.expertInfoList = response.rows;
      });
    },
    /** 查询研究生信息列表 */
    getList() {
      this.loading = true;
      listPostgraduateInfo(this.queryParams).then(response => {
        this.postgraduateInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        postgraduateId: undefined,
        userId: undefined,
        academyId: undefined,
        facultiesId: undefined,
        majorId: undefined,
        expertId: undefined,
        name: undefined,
        gender: undefined,
        nation: undefined,
        birth: undefined,
        idCard: undefined,
        phone: undefined,
        email: undefined,
        politicalAffiliation: undefined,
        homeAddress: undefined,
        photoPath: undefined,
        status: undefined,
        remark: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.postgraduateId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加研究生信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const postgraduateId = row.postgraduateId || this.ids
      getPostgraduateInfo(postgraduateId).then(response => {
        this.loading = false;
        this.form = response.data;
        let sesearchInterests = this.form.sesearchInterests
        this.form.majorfieldList = sesearchInterests.split(",")
        this.selectMajor(this.form.majorId)
        this.open = true;
        this.title = "修改研究生信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          let sesearchInterests = this.form.majorfieldList
          this.form.sesearchInterests = sesearchInterests.join(",")
          if (this.form.postgraduateId != null) {
            updatePostgraduateInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addPostgraduateInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postgraduateIds = row.postgraduateId || this.ids;
      this.$modal.confirm('是否确认删除研究生信息编号为"' + postgraduateIds + '"的数据项？').then(() => {
        this.loading = true;
        return delPostgraduateInfo(postgraduateIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/postgraduateInfo/export', {
        ...this.queryParams
      }, `postgraduateInfo_${new Date().getTime()}.xlsx`)
    },
    selectMajor(e){
      let param = {
        majorId: e
      }
      listMajorfield(param).then(response => {
        this.majorfieldList = response.rows;
      });
    }
  }
};
</script>

<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
